package com.a01_2021.thousandstimes.instance.controller;


import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.general.jwt.JwtTokenUtil;
import com.a01_2021.thousandstimes.instance.entity.Dimission;
import com.a01_2021.thousandstimes.instance.service.IDimissionService;
import com.a01_2021.thousandstimes.utils.AjaxResponse;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author E1xP
 * @since 2021-03-10
 */
@RestController
@RequestMapping("/dimission")
public class DimissionController {

    @Resource
    IDimissionService dimissionService;

    @Resource
    JwtTokenUtil jwtTokenUtil;

    /**
     * 申请离职
     *
     * @param instanceId 实例id
     * @param token      jwt令牌
     * @param reason     申请离职原因
     * @return AjaxResponse
     * @throws CustomException 当已存在有效离职申请时抛出该异常
     */
    @PreAuthorize("hasAuthority('dimission-dimission')")
    @DS("#header.instanceid")
    @PostMapping("dimission")
    public AjaxResponse dimission(@RequestHeader("instanceid") String instanceId, @RequestHeader("${jwt.header}") String token, String reason) throws CustomException {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));
        Dimission dimission = dimissionService.dimission(userId, reason);
        return AjaxResponse.success(dimission);
    }

    /**
     * 判断是否有有效离职申请
     *
     * @param instanceId 实例id
     * @param token      jwt令牌
     * @param userId     用户id
     * @return AjaxResponse
     * @throws CustomException 不应当抛出该异常
     */
    @PreAuthorize("hasAuthority('dimission-isDimission')")
    @DS("#header.instanceid")
    @PostMapping("isDimission")
    public AjaxResponse isDimission(@RequestHeader("instanceid") String instanceId, @RequestHeader("${jwt.header}") String token, long userId) throws CustomException {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        return AjaxResponse.success(dimissionService.isDimission(userId));
    }


    /**
     * 用于获取未处理的离职申请
     *
     * @param instanceId 实例id
     * @return AjaxResponse
     */
    @PreAuthorize("hasAuthority('dimission-getUnHandle')")
    @DS("#header.instanceid")
    @PostMapping("getUnHandle")
    public AjaxResponse getUnHandle(@RequestHeader("instanceid") String instanceId) {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        List<Dimission> dimissionList = dimissionService.getUnHandle();
        return AjaxResponse.success(dimissionList);
    }

    /**
     * 同意离职申请
     *
     * @param instanceId  实例id
     * @param dimissionId 离职申请id
     * @return AjaxResponse
     * @throws CustomException 当离职申请不存在时抛出该异常
     */
    @PreAuthorize("hasAuthority('dimission-agree')")
    @DS("#header.instanceid")
    @PostMapping("agree")
    public AjaxResponse agree(@RequestHeader("instanceid") String instanceId, long dimissionId) throws CustomException {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        Dimission dimission = dimissionService.agree(dimissionId, Long.parseLong(instanceId));
        return AjaxResponse.success(dimission);
    }

    /**
     * 不同意离职申请
     *
     * @param instanceId  实例id
     * @param dimissionId 离职申请id
     * @return AjaxResponse
     * @throws CustomException 当离职申请不存在时抛出该异常
     */
    @PreAuthorize("hasAuthority('dimission-disagree')")
    @DS("#header.instanceid")
    @PostMapping("disagree")
    public AjaxResponse disagree(@RequestHeader("instanceid") String instanceId, long dimissionId) throws CustomException {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        Dimission dimission = dimissionService.disagree(dimissionId);
        return AjaxResponse.success(dimission);
    }

    /**
     * 用于获取自己的离职申请
     *
     * @param instanceId 实例id
     * @param token      jwt令牌
     * @return AjaxResponse
     */
    @PreAuthorize("hasAuthority('dimission-getMyDimission')")
    @DS("#header.instanceid")
    @PostMapping("getMyDimission")
    public AjaxResponse getMyDimission(@RequestHeader("instanceid") String instanceId, @RequestHeader("${jwt.header}") String token) {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));
        List<Dimission> dimissionList = dimissionService.getMyDimission(userId);
        return AjaxResponse.success(dimissionList);
    }
}
